package com.zdb.n1.controller;

import com.zdb.n1.entity.FitnessGoal;
import com.zdb.n1.payload.response.MessageResponse;
import com.zdb.n1.service.FitnessGoalService;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/fitness-goals")
public class FitnessGoalController {

    @Autowired
    private FitnessGoalService fitnessGoalService;

    @PostMapping
    public ResponseEntity<FitnessGoal> createFitnessGoal(@Valid @RequestBody FitnessGoal fitnessGoal) {
        return ResponseEntity.ok(fitnessGoalService.saveFitnessGoal(fitnessGoal));
    }

    @PutMapping("/{id}")
    public ResponseEntity<FitnessGoal> updateFitnessGoal(
            @PathVariable Long id, 
            @Valid @RequestBody FitnessGoal fitnessGoal) {
        FitnessGoal existingGoal = fitnessGoalService.getFitnessGoalById(id);
        fitnessGoal.setId(existingGoal.getId());
        return ResponseEntity.ok(fitnessGoalService.saveFitnessGoal(fitnessGoal));
    }

    @GetMapping("/{id}")
    public ResponseEntity<FitnessGoal> getFitnessGoalById(@PathVariable Long id) {
        return ResponseEntity.ok(fitnessGoalService.getFitnessGoalById(id));
    }

    @GetMapping
    public ResponseEntity<List<FitnessGoal>> getAllFitnessGoals() {
        return ResponseEntity.ok(fitnessGoalService.getAllFitnessGoalsForCurrentUser());
    }

    @GetMapping("/paged")
    public ResponseEntity<Page<FitnessGoal>> getPagedFitnessGoals(Pageable pageable) {
        return ResponseEntity.ok(fitnessGoalService.getFitnessGoalsForCurrentUserPaged(pageable));
    }

    @GetMapping("/by-status/{status}")
    public ResponseEntity<List<FitnessGoal>> getFitnessGoalsByStatus(
            @PathVariable FitnessGoal.GoalStatus status) {
        return ResponseEntity.ok(fitnessGoalService.getFitnessGoalsByStatus(status));
    }

    @GetMapping("/by-type/{type}")
    public ResponseEntity<List<FitnessGoal>> getFitnessGoalsByType(
            @PathVariable FitnessGoal.GoalType type) {
        return ResponseEntity.ok(fitnessGoalService.getFitnessGoalsByType(type));
    }

    @GetMapping("/expired")
    public ResponseEntity<List<FitnessGoal>> getExpiredActiveGoals() {
        return ResponseEntity.ok(fitnessGoalService.getExpiredActiveGoals());
    }

    @GetMapping("/upcoming")
    public ResponseEntity<List<FitnessGoal>> getUpcomingGoals() {
        return ResponseEntity.ok(fitnessGoalService.getUpcomingGoals());
    }

    @GetMapping("/summary")
    public ResponseEntity<Map<String, Object>> getGoalsSummary() {
        Map<String, Object> summary = new HashMap<>();
        
        List<FitnessGoal> activeGoals = fitnessGoalService.getFitnessGoalsByStatus(FitnessGoal.GoalStatus.ACTIVE);
        List<FitnessGoal> completedGoals = fitnessGoalService.getFitnessGoalsByStatus(FitnessGoal.GoalStatus.COMPLETED);
        List<FitnessGoal> expiredGoals = fitnessGoalService.getExpiredActiveGoals();
        List<FitnessGoal> upcomingGoals = fitnessGoalService.getUpcomingGoals();
        
        summary.put("activeCount", activeGoals.size());
        summary.put("completedCount", completedGoals.size());
        summary.put("expiredCount", expiredGoals.size());
        summary.put("upcomingCount", upcomingGoals.size());
        
        return ResponseEntity.ok(summary);
    }

    @PutMapping("/{id}/progress")
    public ResponseEntity<?> updateGoalProgress(
            @PathVariable Long id, 
            @RequestParam Double progress) {
        try {
            fitnessGoalService.updateGoalProgress(id, progress);
            return ResponseEntity.ok( MessageResponse.success("进度更新成功"));
        } catch (Exception e) {
            return ResponseEntity.badRequest().body( MessageResponse.error("更新失败: " + e.getMessage()));
        }
    }

    @PutMapping("/{id}/complete")
    public ResponseEntity<?> completeGoal(@PathVariable Long id) {
        try {
            fitnessGoalService.completeGoal(id);
            return ResponseEntity.ok( MessageResponse.success("目标已成功标记为完成"));
        } catch (Exception e) {
            return ResponseEntity.badRequest().body( MessageResponse.error("标记完成失败: " + e.getMessage()));
        }
    }

    @DeleteMapping("/{id}")
    public ResponseEntity<MessageResponse> deleteFitnessGoal(@PathVariable Long id) {
        return ResponseEntity.ok(fitnessGoalService.deleteFitnessGoal(id));
    }
} 